<!doctype html>
<html lang="zh-CN">
<head>
	<meta charset="utf-8">
	<title>Laravel - 为 WEB 艺术家创造的 PHP 框架。 | Laravel 中文网</title>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="author" content="Laravel 中文网">
	<meta name="description" content="Laravel - 为 WEB 艺术家创造的 PHP 框架。| Laravel 中文网">
	<meta name="keywords" content="Laravel中文社区,php框架,laravel中文网,php framework,restful routing,laravel,laravel php">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--[if lte IE 9]>
		<script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
	<![endif]-->
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="../../assets/css/laravel.css">
</head>
<body class="docs language-php">

	<span class="overlay"></span>

	<nav class="main">
		<div class="container">
			<a href="../../index.html" class="brand">
				<img src="../../assets/img/laravel-logo.png" height="30">
				Laravel
			</a>

			<div class="responsive-sidebar-nav">
				<a href="#" class="toggle-slide menu-link btn">&#9776;</a>
			</div>

				<div class="switcher">
					<div class="dropdown">
						<button class="btn dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-expanded="true">
							<!--<span class="faint">v</span> -->
							4.1
							<span class="caret"></span>
						</button>
						<ul class="dropdown-menu" role="menu" aria-labelledby="dropdownMenu1">
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../5.0/index.html">5.0</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.2/index.html">4.2</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="index.html">4.1</a>
								</li>
								<li role="presentation">
									<a role="menuitem" tabindex="-1" href="../4.0/index.html">4.0</a>
								</li>
						</ul>
					</div>
				</div>

			<ul class="main-nav">
				<li class="nav-docs"><a href="../index.html">中文文档</a></li>
				<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
				<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
				<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
				<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
				
			</ul>
		</div>
	</nav>

		<nav id="slide-menu" class="slide-menu" role="navigation">
		
		<div class="brand">
			<a href="../../index.html">
				<img src="../../assets/img/laravel-logo-white.png" height="50">
			</a>
		</div>

		<ul class="slide-main-nav">
			<li><a href="../../index.html">首页</a></li>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>

		<div class="slide-docs-nav">
			<h2>文档目录</h2>
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="releases.html">Release Notes</a></li>
<li><a href="upgrade.html">Upgrade Guide</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="security.html">Authentication</a></li>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="ssh.html">SSH</a></li>
<li><a href="templates.html">模板</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</div>

	</nav>

	<div class="docs-wrapper container">

		<section class="sidebar">
			<ul>
<li>序言
<ul>
<li><a href="introduction.html">简介</a></li>
<li><a href="quick.html">快速入门</a></li>
<li><a href="releases.html">Release Notes</a></li>
<li><a href="upgrade.html">Upgrade Guide</a></li>
</ul>
</li>
<li>开始学习
<ul>
<li><a href="installation.html">安装</a></li>
<li><a href="configuration.html">配置</a></li>
<li><a href="lifecycle.html">Request的生命周期</a></li>
<li><a href="routing.html">路由</a></li>
<li><a href="requests.html">请求与输入</a></li>
<li><a href="responses.html">视图 &amp; Response</a></li>
<li><a href="controllers.html">控制器</a></li>
<li><a href="errors.html">错误 &amp; 日志</a></li>
</ul>
</li>
<li>深度历险
<ul>
<li><a href="security.html">Authentication</a></li>
<li><a href="cache.html">缓存</a></li>
<li><a href="extending.html">核心扩展</a></li>
<li><a href="events.html">事件</a></li>
<li><a href="facades.html">Facades</a></li>
<li><a href="html.html">表单 &amp; HTML</a></li>
<li><a href="helpers.html">有用的工具函数</a></li>
<li><a href="ioc.html">IoC 容器</a></li>
<li><a href="localization.html">本地化</a></li>
<li><a href="mail.html">邮件</a></li>
<li><a href="packages.html">扩展包开发</a></li>
<li><a href="pagination.html">分页</a></li>
<li><a href="queues.html">队列</a></li>
<li><a href="security.html">安全</a></li>
<li><a href="session.html">Session</a></li>
<li><a href="ssh.html">SSH</a></li>
<li><a href="templates.html">模板</a></li>
<li><a href="testing.html">单元测试</a></li>
<li><a href="validation.html">验证</a></li>
</ul>
</li>
<li>数据库
<ul>
<li><a href="database.html">基本用法</a></li>
<li><a href="queries.html">查询构造器</a></li>
<li><a href="eloquent.html">Eloquent ORM</a></li>
<li><a href="schema.html">结构生成器</a></li>
<li><a href="migrations.html">迁移 &amp; 数据填充</a></li>
<li><a href="redis.html">Redis</a></li>
</ul>
</li>
<li>Artisan 命令行工具
<ul>
<li><a href="artisan.html">概览</a></li>
<li><a href="commands.html">Artisan 开发</a></li>
</ul>
</li>
<li>贡献者
<ul>
<li><a href="contributors.html">中文翻译贡献者名单</a></li>
</ul>
</li>
</ul>

		</section>

		<article>
			<h1>模板</h1>
<ul>
<li><a href="#controller-layouts">控制器布局</a></li>
<li><a href="#blade-templating">Blade模板</a></li>
<li><a href="#other-blade-control-structures">其他 Blade模板 控制结构</a></li>
</ul>
<p><a name="controller-layouts"></a></p>
<h2>控制器布局</h2>
<p>在Laravel框架中使用模板的一种方法就是通过控制器布局。通过在控制器中指定 <code>layout</code> 属性，对应的视图会被创建并且作为请求的默认返回数据。</p>
<p><strong>在控制器中定义一个布局</strong></p>
<pre><code>class UserController extends BaseController {

    /**
     * The layout that should be used for responses.
     */
    protected $layout = 'layouts.master';

    /**
     * Show the user profile.
     */
    public function showProfile()
    {
        $this-&gt;layout-&gt;content = View::make('user.profile');
    }

}
</code></pre>
<p><a name="blade-templating"></a></p>
<h2>Blade模板</h2>
<p>Blade是Laravel框架下的一种简单又强大的模板引擎。
不同于控制器布局，Blade模板引擎由模板继承和模板片段驱动。所有的Blade模板文件必须使用Blade <code>.blade.php</code> 文件扩展名。</p>
<p><strong>定义一个Blade布局</strong></p>
<pre><code>&lt;!-- Stored in app/views/layouts/master.blade.php --&gt;

&lt;html&gt;
    &lt;body&gt;
        @section('sidebar')
            This is the master sidebar.
        @show

        &lt;div class=&quot;container&quot;&gt;
            @yield('content')
        &lt;/div&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><strong>使用一个Blade布局</strong></p>
<pre><code>@extends('layouts.master')

@section('sidebar')
    @parent

    &lt;p&gt;This is appended to the master sidebar.&lt;/p&gt;
@stop

@section('content')
    &lt;p&gt;This is my body content.&lt;/p&gt;
@stop
</code></pre>
<p>注意一个Blade布局的扩展视图简单地在布局中替换了模板片段。通过在模板片段中使用 <code>@parent</code> 指令，布局的内容可以被包含在一个子视图中，这样你就可以在布局片段中添加诸如侧边栏、底部信息的内容。</p>
<p>Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the <code>@yield</code> directive. You may pass the default value as the second argument:</p>
<pre><code>@yield('section', 'Default Content');
</code></pre>
<p><a name="other-blade-control-structures"></a></p>
<h2>其他 Blade模板 控制结构</h2>
<p><strong>输出数据</strong></p>
<pre><code>Hello, {{ $name }}.

The current UNIX timestamp is {{ time() }}.
</code></pre>
<p><strong>Echoing Data After Checking For Existence</strong></p>
<p>Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:</p>
<pre><code>{{ isset($name) ? $name : 'Default' }}
</code></pre>
<p>However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:</p>
<pre><code>{{ $name or 'Default' }}
</code></pre>
<p><strong>Displaying Raw Text With Curly Braces</strong></p>
<p>If you need to display a string that is wrapped in curly braces, you may escape the Blade behavior by prefixing your text with an <code>@</code> symbol:</p>
<pre><code>@{{ This will not be processed by Blade }}
</code></pre>
<p>Of course, all user supplied data should be escaped or purified. To escape the output, you may use the triple curly brace syntax:</p>
<pre><code>Hello, {{{ $name }}}.
</code></pre>
<blockquote>
<p><strong>Note:</strong> Be very careful when echoing content that is supplied by users of your application. Always use the triple curly brace syntax to escape any HTML entities in the content.</p>
</blockquote>
<p><strong>If 声明</strong></p>
<pre><code>@if (count($records) === 1)
    I have one record!
@elseif (count($records) &gt; 1)
    I have multiple records!
@else
    I don't have any records!
@endif

@unless (Auth::check())
    You are not signed in.
@endunless
</code></pre>
<p><strong>循环</strong></p>
<pre><code>@for ($i = 0; $i &lt; 10; $i++)
    The current value is {{ $i }}
@endfor

@foreach ($users as $user)
    &lt;p&gt;This is user {{ $user-&gt;id }}&lt;/p&gt;
@endforeach

@while (true)
    &lt;p&gt;I'm looping forever.&lt;/p&gt;
@endwhile
</code></pre>
<p><strong>包含子视图</strong></p>
<pre><code>@include('view.name')
</code></pre>
<p>You may also pass an array of data to the included view:</p>
<pre><code>@include('view.name', array('some'=&gt;'data'))
</code></pre>
<p><strong>Overwriting Sections</strong></p>
<p>By default, sections are appended to any previous content that exists in the section. To overwrite a section entirely, you may use the <code>overwrite</code> statement:</p>
<pre><code>@extends('list.item.container')

@section('list.item.content')
    &lt;p&gt;This is an item of type {{ $item-&gt;type }}&lt;/p&gt;
@overwrite
</code></pre>
<p><strong>输出多语言（Language Lines）</strong></p>
<pre><code>@lang('language.line')

@choice('language.line', 1);
</code></pre>
<p><strong>注释</strong></p>
<pre><code>{{-- This comment will not be in the rendered HTML --}}
</code></pre>
<p><a name="extending-blade"></a></p>
<h2>扩展 Blade</h2>
<p>Blade even allows you to define your own custom control structures. When a Blade file is compiled, each custom extension is called with the view contents, allowing you to do anything from simple <code>str_replace</code> manipulations to more complex regular expressions.</p>
<p>The Blade compiler comes with the helper methods <code>createMatcher</code> and <code>createPlainMatcher</code>, which generate the expression you need to build your own custom directives.</p>
<p>The <code>createPlainMatcher</code> method is used for directives with no arguments like <code>@endif</code> and <code>@stop</code>, while <code>createMatcher</code> is used for directives with arguments.</p>
<p>The following example creates a <code>@datetime($var)</code> directive which simply calls <code>-&gt;format()</code> on <code>$var</code>:</p>
<pre><code>Blade::extend(function($view, $compiler)
{
    $pattern = $compiler-&gt;createMatcher('datetime');

    return preg_replace($pattern, '$1&lt;?php echo $2-&gt;format(\'m/d/Y H:i\'); ?&gt;', $view);
});
</code></pre>

		</article>
	</div>


	<footer class="main">
		<ul>
			<li class="nav-docs"><a href="../index.html">中文文档</a></li>
			<li class="nav-community"><a href="http://wenda.golaravel.com" target="_blank">问答社区</a></li>
			<li class="nav-api"><a href="http://laravel.com/api/5.0/" target="_blank">API</a></li>
			<li class="nav-laracasts"><a href="https://laracasts.com" target="_blank">视频教程（国外）</a></li>
			<li class="nav-forge"><a href="https://forge.laravel.com" target="_blank">Forge</a></li>
			
		</ul>
		<p>Laravel is a trademark of Taylor Otwell. Copyright &copy; Taylor Otwell.</p>
		<p class="less-significant"><a href="http://jackmcdade.com" target="_blank">Design by Jack McDade</a></p>
	</footer>

	<script src="../../assets/js/laravel.js"></script>
	<script src="../../assets/js/viewport-units-buggyfill.js"></script>
	<script>window.viewportUnitsBuggyfill.init();</script>
	<script type="text/javascript">
	var _bdhmProtocol = (("https:" == document.location.protocol) ? " https://" : " http://");
	document.write(unescape("%3Cscript src='" + _bdhmProtocol + "hm.baidu.com/h.js%3Fc8d13872a523d9c286aa7affbe0921f1' type='text/javascript'%3E%3C/script%3E"));
	</script>
</body>
</html>
